package com.panpa.yellcall;


import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import  android.graphics.drawable.Drawable;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import android.app.*;
import android.widget.AdapterView.*;
import android.widget.*;
import android.content.*;
import java.util.*;
import android.support.v7.app.*;
import android.support.v7.preference.*;

class AppInfo{
    public String appName="";
    public String packageName="";
    public String versionName="";
    public int versionCode=0;
    public Drawable appIcon=null;

    public void print(){
        Log.v("app","Name:"+appName+" Package:"+packageName);
        Log.v("app","Name:"+appName+" versionName:"+versionName);
        Log.v("app","Name:"+appName+" versionCode:"+versionCode);
    }
}
public class AppList extends AppCompatActivity
{


	private ListView listView;

	private AppInfo tmpInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_list);
        try{
            final List<AppInfo> appList=new ArrayList<>();
            List<PackageInfo> packages=getPackageManager().getInstalledPackages(0);
            for(int i=0;i<packages.size();++i){
                PackageInfo packageInfo=packages.get(i);
				tmpInfo=new AppInfo();
                tmpInfo.appName=packageInfo.applicationInfo.loadLabel(getPackageManager()).toString();
                tmpInfo.packageName=packageInfo.packageName;
                tmpInfo.versionName=packageInfo.versionName;
                tmpInfo.versionCode=packageInfo.versionCode;
                tmpInfo.appIcon=packageInfo.applicationInfo.loadIcon(getPackageManager());

                //获取非系统应用
                if((packageInfo.applicationInfo.flags& ApplicationInfo.FLAG_SYSTEM)==0){
                    appList.add(tmpInfo);
                }
                //appList.add(tmpInfo);
            }
            listView=(ListView)findViewById(R.id.list);
            MyAdapter arrayAdapter=new MyAdapter(this,R.layout.list_item,appList);
            listView.setAdapter(arrayAdapter);
			listView.setOnItemClickListener(new OnItemClickListener(){

					@Override
					public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
					{

						SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(AppList.this);
						prefs.edit().putString("packageName",appList.get(p3).packageName).commit();
						Toast.makeText(AppList.this,"选择了:"+appList.get(p3).appName ,Toast.LENGTH_SHORT).show();

						// TODO: Implement this method

					}
				});
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

class MyAdapter extends ArrayAdapter<AppInfo>{
    private Context mContext;
    private int resource;
    public MyAdapter(Context context,int resource,List<AppInfo> data){
        super(context,resource,data);
        this.mContext=context;
        this.resource=resource;
    }

    @Override
    public View getView(int position,View convertView,ViewGroup parent){
        ViewHolder holder=null;

        if(convertView==null){
            holder=new ViewHolder();
            convertView= LayoutInflater.from(mContext).inflate(resource,null);
            holder.image=(ImageView)convertView.findViewById(R.id.ItemImage);
            holder.title=(TextView)convertView.findViewById(R.id.ItemTitle);
            holder.text=(TextView)convertView.findViewById(R.id.ItemText);
            convertView.setTag(holder);
        }else{
            holder=(ViewHolder)convertView.getTag();
        }

        holder.image.setImageDrawable(getItem(position).appIcon);
        holder.title.setText(getItem(position).appName);
        holder.text.setText(getItem(position).packageName);

        return convertView;
    }
    final class ViewHolder{
        ImageView image;
        TextView title;
        TextView text;
    }
}
